package cn.tedu.mall.order.service.impl;

import cn.tedu.mall.common.exception.CoolSharkServiceException;
import cn.tedu.mall.common.pojo.domain.CsmallAuthenticationInfo;
import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.ResponseCode;
import cn.tedu.mall.order.mapper.OmsCartMapper;
import cn.tedu.mall.order.service.IOmsCartService;
import cn.tedu.mall.pojo.order.dto.CartAddDTO;
import cn.tedu.mall.pojo.order.dto.CartUpdateDTO;
import cn.tedu.mall.pojo.order.model.OmsCart;
import cn.tedu.mall.pojo.order.vo.CartStandardVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OmsCartServiceImpl implements IOmsCartService {

    @Autowired
    private OmsCartMapper omsCartMapper;

    @Override
    public void addCart(CartAddDTO cartDTO) {
        //1.获取登录用户id
        Long userId = getUserId();
        //2.查询当前用户中的购物车中是否有指定的sku
        OmsCart omsCart = omsCartMapper.selectExistsCart(userId, cartDTO.getSkuId());
        if(omsCart==null){
            //3.购物车没有该商品，新增sku到购物车中
            OmsCart newOmsCart = new OmsCart();
            BeanUtils.copyProperties(cartDTO,newOmsCart);
            newOmsCart.setUserId(userId);
            omsCartMapper.addCart(newOmsCart);
        }else{
            //4.修改购物车中商品的数量
            omsCart.setQuantity(omsCart.getQuantity()+cartDTO.getQuantity());
            omsCartMapper.updateQuantityById(omsCart);
        }
    }

    @Override
    public JsonPage<CartStandardVO> listCarts(Integer page, Integer pageSize) {
        //获取userId
        Long userId = getUserId();
        //进行分页查询
        //设置分页条件
        PageHelper.startPage(page,pageSize);
        //执行查询
        List<CartStandardVO> cartStandardVOS = omsCartMapper.selectCartsByUserId(userId);
        //将分页数据和分页信息进行返回
        return JsonPage.restPage(new PageInfo<>(cartStandardVOS));
    }

    @Override
    public void removeCart(Long[] ids) {
        int rows = omsCartMapper.deleteCartsByIds(ids);
        //rows==0，表示删除失败
        if (rows==0)
            throw new CoolSharkServiceException(ResponseCode.NOT_FOUND,"您要删除的商品不存在");
    }

    @Override
    public void removeAllCarts() {
        Long userId = getUserId();
        int rows = omsCartMapper.deleteCartsByUserId(userId);
        if (rows==0)
            throw new CoolSharkServiceException(ResponseCode.NOT_FOUND,"您的购物车中没有商品");
    }

    @Override
    public void removeUserCarts(OmsCart omsCart) {
        omsCartMapper.deleteCartsByUserIdAndSkuId(omsCart);
    }

    @Override
    public void updateQuantity(CartUpdateDTO cartUpdateDTO) {
        OmsCart omsCart = new OmsCart();
        BeanUtils.copyProperties(cartUpdateDTO,omsCart);
        omsCartMapper.updateQuantityById(omsCart);
    }

    //获取用户信息
    public CsmallAuthenticationInfo getUserInfo(){
        //通过Security请求上下文对象获取的token
        UsernamePasswordAuthenticationToken token =
                (UsernamePasswordAuthenticationToken)
                        SecurityContextHolder.getContext().getAuthentication();
        //判断token是否为空
        if(token==null)
            throw new CoolSharkServiceException(ResponseCode.UNAUTHORIZED,"没有登录信息");
        //从token中获取用户信息
        CsmallAuthenticationInfo userInfo = (CsmallAuthenticationInfo) token.getCredentials();
        return userInfo;
    }
    //获取userId
    public Long getUserId(){
        return getUserInfo().getId();
    }
}
